﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		FlipEffectConfig.cs
//
//	Author:		MJP
//
//	Date:		05/07/09
//
//======================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing.Design;
using System.Windows.Forms.Design;

using CPXMenu;

using JSButton = CPXMenu.Button;

namespace CPXMenu
{
	/// <summary>
	/// A dialog that allows the user to select a PageFlipEffect, and configure it
	/// </summary>
    internal partial class FlipEffectConfig : Form
    {
        private PageFlipEffect selectedEffect;

		/// <summary>
		/// The PageFlipEffect that the user selected
		/// </summary>
        public PageFlipEffect SelectedEffect
        {
            get { return selectedEffect; }
        }

		/// <summary>
		/// Creates an instance of PageFlipEffect
		/// </summary>
		/// <param name="currentEffect">The PageFlipEffect currently in use, the dialog will select its type and match its settings</param>
        public FlipEffectConfig(PageFlipEffect currentEffect)
        {
            InitializeComponent();

            Type[] types = PageFlipEffect.GetPageFlipEffectTypes();
            foreach (Type type in types)
            {
                if (!type.IsAbstract)
                {
                    PageFlipEffect effect;
                    if (type == currentEffect.GetType())
                        effect = currentEffect;
                    else
                        effect = (PageFlipEffect)Activator.CreateInstance(type);

                    listBox.Items.Add(effect);

                    if (effect == currentEffect)
                        listBox.SelectedItem = effect;
                }
            }

            if (listBox.SelectedItem == null)
                listBox.SelectedIndex = 0;
        }

		/// <summary>
		/// Called when the SelectedIndex of the ListBox changes
		/// </summary>
		/// <param name="sender">The sending ListBox</param>
		/// <param name="e">Event arguments</param>
        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox.SelectedItem != null)
            {
                PageFlipEffect effect = (PageFlipEffect)listBox.SelectedItem;
                textBox.Text = effect.Description;
                propertyGrid.SelectedObject = effect;
                selectedEffect = effect;
            }
            else
            {
                textBox.Text = "";
                propertyGrid.SelectedObject = null;                
            }
        }
    }

    /// <summary>
    /// UITypeEditor for Page properties that uses PagePicker to let the user select pages
    /// </summary>
    public class FlipEffectConfigEditor : UITypeEditor
    {
        /// <summary>
        /// Gets the style that will be used for editing. Always returns DropDown.
        /// </summary>
        /// <param name="context">The editing context</param>
        /// <returns>The edit style to use</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        /// <summary>
        /// Allows the user to edit a value by popping up PagePicker
        /// </summary>
        /// <param name="context">The editing context</param>
        /// <param name="provider">The service provider</param>
        /// <param name="value">The current value</param>
        /// <returns>The value selected by the user</returns>
        public override object EditValue(ITypeDescriptorContext context,
                                            IServiceProvider provider,
                                            object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                JSButton button = (JSButton)context.Instance;
                FlipEffectConfig configDialog = new FlipEffectConfig(button.PageFlipEffect);

                if (editorService.ShowDialog(configDialog) == DialogResult.OK)
                    value = configDialog.SelectedEffect;
            }

            return value;
        }
    }
}
